Skip to content

geotiff: validate tile_size and chunks size args (#1752)#1757

Open
brendancol wants to merge 1 commit into
mainfrom
issue-1752
Open

geotiff: validate tile_size and chunks size args (#1752)#1757
brendancol wants to merge 1 commit into
mainfrom
issue-1752

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

  • to_geotiff(..., tiled=True, tile_size=0) reached the tiled writer where math.ceil(width / tile_size) raised ZeroDivisionError with no indication that tile_size was the bad input. Negative values produced a nonsensical tile grid.
  • read_geotiff_dask(chunks=0) (or chunks=(0, N)) propagated zero into dask's chunk math and surfaced as a confusing range() / empty-chunks error.
  • Add up-front validation at both entry points. Reject non-positive ints, non-int values, and malformed chunks tuples with a ValueError that names the parameter and the bad value. tiled=False skips the tile_size check since the writer ignores tile_size in strip mode.
  • Style matches the existing window= validator in read_geotiff_dask.

Closes #1752.

Test plan

  • New test_size_param_validation_1752.py (11 tests): tile_size 0 / negative / non-int rejected, tile_size=1 still writes; chunks 0 / negative / (0, N) / (N, -1) / 3-tuple rejected; chunks=256 and chunks=(4, 8) still read.
  • Verified tests fail on main (8 of the 11 raise wrong error / no error) and pass with the fix.
  • Full geotiff suite: 2132 passed; the 12 pre-existing failures (matplotlib RecursionError in test_features.py, GPU test_predictor2_big_endian_gpu_1517.py, GPU test_no_georef_windowed_coords_1710.py) reproduce on main without this change.

`to_geotiff(..., tiled=True, tile_size=0)` reached the tiled writer
where `math.ceil(width / tile_size)` raised `ZeroDivisionError` without
naming `tile_size`. `read_geotiff_dask(chunks=0)` (or `chunks=(0, N)`)
propagated zero into dask's chunk math and surfaced as a confusing
`range()` / empty-chunks error.

Add up-front checks at both entry points that reject non-positive ints,
non-int values, and malformed `chunks` tuples with a `ValueError` that
names the parameter and the bad value. Style matches the existing
`window=` validator in `read_geotiff_dask`.

Closes #1752.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label May 12, 2026
@brendancol brendancol requested a review from Copilot May 12, 2026 23:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the GeoTIFF API surface by validating size-related parameters (tile_size for tiled writes and chunks for dask reads) early, so invalid inputs fail with clear ValueErrors rather than confusing downstream exceptions.

Changes:

  • Add up-front tile_size validation in to_geotiff when tiled=True.
  • Add up-front chunks validation in read_geotiff_dask for scalar and (row, col) tuple forms.
  • Add regression tests covering invalid/valid combinations for both entry points.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
xrspatial/geotiff/__init__.py Adds parameter validation for tile_size (tiled writes) and chunks (dask reads) to improve error clarity and prevent downstream failures.
xrspatial/geotiff/tests/test_size_param_validation_1752.py Introduces regression tests ensuring invalid sizes raise ValueError and valid sizes continue to work.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1929 to +1935
# was the problem. ``chunks`` may be an int or a (row, col) tuple.
if isinstance(chunks, int) and not isinstance(chunks, bool):
if chunks <= 0:
raise ValueError(
f"chunks must be a positive int or (row, col) tuple of "
f"positive ints, got chunks={chunks}.")
elif isinstance(chunks, tuple):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

geotiff: validate tile_size and dask chunks at entry points

2 participants